package com.xrit.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xrit.constant.MessageConstant;
import com.xrit.exception.CustomException;
import com.xrit.pojo.OrderSetting;
import com.xrit.result.Result;
import com.xrit.service.OrderSettingService;
import com.xrit.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;


    @RequestMapping("/findDataByYearAndMonth")
    public Result findDataByYearAndMonth(Integer year,Integer month){
        List<Map> maps = orderSettingService.queryDataByYearAndMonth(year, month);
        if (maps!=null){
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,maps);
        }
        return new Result(true,MessageConstant.GET_ORDERSETTING_ISNULL);
    }

        //upload
    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile) throws CustomException {
        try {
            if (excelFile!=null && !excelFile.isEmpty()){
                //1.读取excel文件
                List<String[]> list = POIUtils.readExcel(excelFile);
                //2.将List<String[]>转成List<OrderSetting>
                List<OrderSetting> orderSettings=new ArrayList<>();
                for (String [] strings:list){
                    OrderSetting os=new OrderSetting();
                    os.setOrderdate(new Date(strings[0]));
                    os.setNumber(Integer.parseInt(strings[1]));
                    orderSettings.add(os);
                }
                //3.添加到数据库中
                orderSettingService.addBatch(orderSettings);
                return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
    }

}
